﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ECOSYSTEM_Dominio;

namespace ECOSYSTEM_DAL
{
    public class PermisoDAL
    {
        SqlConnection cnn;
        public Boolean guardarPermiso(Permiso elPermiso) {
            try
            {
                cnn = (SqlConnection)ConexionSQL.getInstancia().getConexion();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("RegistrarPermiso", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@PermisoID", SqlDbType.Int);
                cmd.Parameters["@PermisoID"].Direction = ParameterDirection.InputOutput;
                cmd.Parameters.Add("@IDPermiso", SqlDbType.NVarChar,10);
                cmd.Parameters.Add("@CargoID", SqlDbType.Int);
                cmd.Parameters["@PermisoID"].Value = elPermiso.PermisoID;
                cmd.Parameters["@IDPermiso"].Value = elPermiso.NomPermiso;
                cmd.Parameters["@CargoID"].Value = elPermiso.ElCargo;
                cmd.ExecuteNonQuery();
                elPermiso.PermisoID = (Int32)(cmd.Parameters["@PermisoID"].Value);
            }
            catch (SqlException ex) { throw new Exception(ex.Message); }
            catch (Exception e) { throw new Exception(e.Message); }
            finally {
                if (cnn.State == ConnectionState.Open) {
                    cnn.Close();
                }
            }
            return true;
        }
    }
}
